import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _class, _class2;

import React from 'react';
import { Button, Row } from 'antd'; // import queryString from 'query-string';

import { Bind } from 'lodash-decorators';
import formatterCollections from "../../utils/intl/formatterCollections";
import intl from "../../utils/intl";
import { getEnvConfig } from "../../utils/iocUtils";
import { removeAccessToken, removeAllCookie, setSession, getSession, extractAccessTokenFromHash, extractRefreshTokenFromHash, setAccessToken, setRefreshToken } from "../../utils/utils";
import styles from "./index.less?modules";
import unauthorized from "../../assets/unauthorized/unauthorized.svg";
var TokenExpired = (_dec = formatterCollections({
  code: ['cpaas.common']
}), _dec2 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(TokenExpired, _React$Component);

  var _super = _createSuper(TokenExpired);

  function TokenExpired() {
    _classCallCheck(this, TokenExpired);

    return _super.apply(this, arguments);
  }

  _createClass(TokenExpired, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props = this.props,
          location = _this$props.location,
          history = _this$props.history;
      var token = extractAccessTokenFromHash(window.location.hash);
      var refreshToken = extractRefreshTokenFromHash(window.location.hash);

      if (token) {
        setAccessToken(token, 60 * 60);
        setRefreshToken(refreshToken, 60 * 60); // 保留上次退出时的页面路径和search

        if (location.pathname === '/public/unauthorized') {
          history.push({
            pathname: '/workplace'
          });
        } else {
          history.push({
            pathname: location.pathname,
            search: location.search
          });
        }
      }
    }
  }, {
    key: "handleReLogin",
    value: function handleReLogin() {
      var cacheLocation = getSession('redirectUrl');
      removeAccessToken();
      removeAllCookie();

      var _getEnvConfig = getEnvConfig(),
          LOGIN_URL = _getEnvConfig.LOGIN_URL;

      setSession('isErrorFlag', false); // 由于 LOGIN_URL 可以 配置, 所以 做一次判断

      if (LOGIN_URL.includes('?')) {
        window.location.href = "".concat(LOGIN_URL, "&redirect_uri=").concat(cacheLocation); // 401 需要在登录后返回401的页面
      } else {
        window.location.href = "".concat(LOGIN_URL, "?redirect_uri=").concat(cacheLocation); // 401 需要在登录后返回401的页面
      }
    }
  }, {
    key: "render",
    value: function render() {
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Row, {
        type: "flex",
        justify: "center"
      }, /*#__PURE__*/React.createElement("img", {
        alt: "",
        src: unauthorized,
        style: {
          width: 400,
          marginTop: 100,
          color: '#003266'
        }
      })), /*#__PURE__*/React.createElement(Row, {
        type: "flex",
        justify: "center"
      }, /*#__PURE__*/React.createElement("h1", {
        style: {
          marginTop: 20,
          marginLeft: 32
        }
      }, intl.get('cpaas.common.view.message.title.unauthorized.expired').d('抱歉，您未登录或身份认证已失效，请点击下方按钮重新登录！'))), /*#__PURE__*/React.createElement(Row, {
        type: "flex",
        justify: "center"
      }, /*#__PURE__*/React.createElement("div", {
        className: styles['button-center']
      }, /*#__PURE__*/React.createElement(Button, {
        onClick: this.handleReLogin,
        size: "large",
        style: {
          backgroundColor: '#003266',
          textAlign: 'center',
          marginTop: 20,
          width: 200,
          color: 'white',
          height: 38,
          fontSize: 15
        }
      }, intl.get('cpaas.common.button.reLogin').d('重新登录')))));
    }
  }]);

  return TokenExpired;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "handleReLogin", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handleReLogin"), _class2.prototype)), _class2)) || _class);
export { TokenExpired as default };